﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.TouringParty;

namespace EFong.View.Financing
{
    public partial class Frm_TouringPartySettlementEdit : BasicForm
    {
        private Proxy.FinanceServiceProxy _financeProxy {get{ return Proxy.FinanceServiceProxy.GetInstance();}}
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}

        public Frm_TouringPartySettlementEdit()
        {
            InitializeComponent();
        }

        protected override void InitFields()
        {
            base.InitFields();
        }

        protected override void Events()
        {
            base.Events();

            this.bbt_Cancel.Click += (s, e) =>
                {
                    this.Close();
                };
            this.bbt_OK.Click += (s, e) =>
                {
                    paySettlement();
                };
            this.bbt_Browser.Click += (s, e) =>
                {
                    browserSettlement();
                };
        }

        protected override void LoadDatas()
        {
            base.LoadDatas();
            bds_Vendor.BindingData(_masterProxy.GetAllVendor().ReturnValue);
            cmb_BankAccount.BindingData(_masterProxy.GetAllBankAccount().ReturnValue);
        }


        private void browserSettlement()
        {
            if (Settlement == null)
                return;

            Frm_TouringPartyEdit frm = new Frm_TouringPartyEdit();
            frm.Text = "订单资料";
            frm.ViewState = ViewStates.BROWSER;
            FormOpener.ShowDialog(frm);
        }

        private void paySettlement()
        {
            UIOperManager.SaveData(() => { return true; },
                () =>
                {
                    return _financeProxy.TourPartyVendorSettlement(Settlement, bds_Detail.DataSource as List<Contract.VendorSettlement>, ckb_IsComplete.GetValue()).IsSuccess;
                },
                "支付成功.",
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                });
        }

        private void updateTotalFields()
        {
            txt_TotalPrice.SetText(Settlement.TotalPrice);
            txt_PayedPrice.SetText(Settlement.PayedPrice);
            txt_UnPayPrice.SetText(Settlement.TotalPrice - Settlement.PayedPrice);
        }

        private Contract.TourPartySettlement _settlement;
        public Contract.TourPartySettlement Settlement
        {
            get { return _settlement; }
            set
            {
                _settlement = value;
                onSettlemenetChanged();
            }
        }

        private void onSettlemenetChanged()
        {
            if (Settlement == null)
            {

            }
            else
            {
                bds_PayedDetail.BindingData(Settlement.PayedDetail);
                updateTotalFields();
            }
        }
    }
}
